package com.zhkj.txg.module.home.ui

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.View
import android.webkit.WebView
import androidx.lifecycle.Observer
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.config.*
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.txg.R
import com.zhkj.txg.module.home.vm.WelfareViewModel
import com.zhkj.txg.module.product.ui.WatcherActivity
import com.zhkj.txg.utils.WebViewUtils
import com.zhkj.txg.utils.WebViewUtils.addImageClickListener
import kotlinx.android.synthetic.main.activity_welfare.*

class WelfareActivity : ViewModelActivity<WelfareViewModel>() {

    companion object {
        private val TYPE = "type"
        private val ID = "id"
        private val TITLE = "title"

        fun welfare(activity: Activity) {
            val intent = Intent(activity, WelfareActivity::class.java)
                .putExtra(TYPE, 0)
            activity.startActivity(intent)
        }

        fun guide(activity: Context, id: Long, title: String) {
            val intent = Intent(activity, WelfareActivity::class.java)
                .putExtra(TYPE, 1).putExtra(ID, id).putExtra(TITLE, title)
            activity.startActivity(intent)
        }

        fun policy(activity: Activity, title: String) {
            val intent = Intent(activity, WelfareActivity::class.java)
                .putExtra(TYPE, 2).putExtra(TITLE, title)
            activity.startActivity(intent)
        }

        fun agreement(activity: Activity, title: String) {
            val intent = Intent(activity, WelfareActivity::class.java)
                .putExtra(TYPE, 3).putExtra(TITLE, title)
            activity.startActivity(intent)
        }

        fun aboutUs(activity: Activity, title: String) {
            val intent = Intent(activity, WelfareActivity::class.java)
                .putExtra(TYPE, 4).putExtra(TITLE, title)
            activity.startActivity(intent)
        }

        fun merchant(activity: Activity, title: String) {
            val intent = Intent(activity, WelfareActivity::class.java)
                .putExtra(TYPE, 5).putExtra(TITLE, title)
            activity.startActivity(intent)
        }
    }

    override fun setViewModel(): WelfareViewModel = createViewModel()
    override fun statusTitleBar(): View? = titleWelfare
    override val layoutResId: Int = R.layout.activity_welfare

    private val webView: WebView by lazy {
        WebView(this)
    }

    override fun initData() {
        when (intent.getIntExtra(TYPE, 0)) {
            0 -> viewModel.welfare()
//            1 -> viewModel.GuideDetail(intent.getLongExtra(ID, 0))
            1 -> {
                titleWelfare.setTitleTextContent(intent.getStringExtra(TITLE) ?: "")
                webView.loadUrl("${GUIDE_URL}${intent.getLongExtra(ID, 0)}")
            }
            2 -> {
                titleWelfare.setTitleTextContent(intent.getStringExtra(TITLE) ?: "")
                webView.loadUrl(PRIVACY_URL)
            }
            3 -> {
                titleWelfare.setTitleTextContent(intent.getStringExtra(TITLE) ?: "")
                webView.loadUrl(USER_URL)
            }
            4 -> {
                titleWelfare.setTitleTextContent(intent.getStringExtra(TITLE) ?: "")
                webView.loadUrl(ABOUT_URL)
            }
            5 -> {
                titleWelfare.setTitleTextContent(intent.getStringExtra(TITLE) ?: "")
                webView.loadUrl(MERCHANT_URL)
            }
        }
    }

    override fun initUI() {
        WebViewUtils.initWebView(webView)
        WebViewUtils.addWebView2Container(webView, container)
        webView.addImageClickListener {
            if (!it.isNullOrEmpty()) WatcherActivity.open(this, arrayListOf(it), 0)
        }
    }

    override fun initViewModel() {
        viewModel.welfareLiveData.observe(this, Observer { response ->
            handlerResponseStatus(response.status, response.statusTip)
            if (response.status == HttpResponseStatus.Status.SUCCESS) response.response?.data?.let {
                titleWelfare.setTitleTextContent(it.title)
                WebViewUtils.loadFullScreenHtml(webView, it.content)
            } else if (response.status == HttpResponseStatus.Status.FAILURE) finish()
        })
        viewModel.guideDetailLiveData.observe(this, Observer { response ->
            handlerResponseStatus(response.status, response.statusTip)
            if (response.status == HttpResponseStatus.Status.SUCCESS) response.response?.data?.let {
                titleWelfare.setTitleTextContent(it.title)
                if (!it.content.isNullOrEmpty()) WebViewUtils.loadFullScreenHtml(webView, it.content)
            } else if (response.status == HttpResponseStatus.Status.FAILURE) finish()
        })
    }

    override fun onDestroy() {
        WebViewUtils.removeWebView(webView, container)
        super.onDestroy()
    }
}